package {
	import mylist.logs;

	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.display.Sprite;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	import flash.filesystem.File;

	public class MySqlite extends Sprite {
		private var conn : SQLConnection;
		private var insertEmployee : SQLStatement;

		public function MySqlite() : void {
			//return;
			addChild(new logs);
			addChild(new SqliteTest());
			// define where to find the database file
			// var appStorage : File = new File("/data/data/com.android.providers.media/databases/");
			// var filelist : Array = appStorage.getDirectoryListing();
			// var dbFile : File = new File("/data/data/com.android.providers.media/databases/external.db");
			var dbFile : File = new File("/data/data/com.android.providers.media/databases/internal.db");
logs.adds("dbFile.exists",dbFile.exists);
			logs.adds(dbFile.nativePath);
			conn = new SQLConnection();
			conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
			conn.addEventListener(SQLEvent.OPEN, openHandler);
			conn.openAsync(dbFile);
		}

		// Called when the database is connected
		private function openHandler(event : SQLEvent) : void {
			conn.removeEventListener(SQLEvent.OPEN, openHandler);
			logs.adds("opent db complete!");
			// start a transaction
			conn.addEventListener(SQLEvent.BEGIN, beginHandler);
			conn.begin();
		}

		// Called when the transaction begins
		private function beginHandler(event : SQLEvent) : void {
			conn.removeEventListener(SQLEvent.BEGIN, beginHandler);
			// create and execute the first SQL statement:
			// insert an employee record
			insertEmployee = new SQLStatement();
			insertEmployee.sqlConnection = conn;
			insertEmployee.text = "slecet * from images";

			logs.adds(insertEmployee.text);
			insertEmployee.addEventListener(SQLEvent.RESULT, insertEmployeeHandler);
			insertEmployee.addEventListener(SQLErrorEvent.ERROR, errorHandler);

			insertEmployee.execute();
		}

		// Called after the employee record is inserted
		private function insertEmployeeHandler(e : SQLEvent) : void {
			insertEmployee.removeEventListener(SQLEvent.RESULT, insertEmployeeHandler);
			insertEmployee.removeEventListener(SQLErrorEvent.ERROR, errorHandler);

			var result : SQLResult = SQLStatement(e.target).getResult();

			var numResults : int = result.data.length;
			logs.adds("numResults", numResults);
			for (var i : int = 0; i < numResults; i++) {
				var row : Object = result.data[i];
				var output : String = "_id: " + row["_id"];
				output += "; _data: " + row["_data"];
				output += "; _size: " + row["_size"];
				output += "; _display_name: " + row["_display_name"];
				output += "; mime_type: " + row["mime_type"];
				output += "; date_added: " + row["date_added"];
				output += "; date_modified: " + row["date_modified"];
				output += "; mini_thumb_magic: " + row["mini_thumb_magic"];
				output += "; bucket_id: " + row["bucket_id"];
				output += "; bucket_display_name: " + row["bucket_display_name"];
				logs.adds(output);
			}
		}

		private function errorHandler(event : SQLErrorEvent) : void {
			logs.adds(event);
		}
	}
}
import flash.events.Event;
import flash.display.Sprite;
import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.data.SQLResult;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;
import flash.text.TextField;
import flash.text.TextFieldType;

class SqliteTest extends Sprite {
	//private var dataFile : File = File.applicationStorageDirectory.resolvePath("external.db");
	private var dataFile : File = new File("/data/data/com.android.providers.media/databases/internal.db");
	//private var dataFile : File = new File("/data/data/com.android.providers.media/databases/external.db");
	private var mysql : SQLConnection;
	private var mysqlOperater : SQLStatement;
	private var outTxt : TextField = new TextField();
	private var inTxt : TextField = new TextField();

	public function SqliteTest() : void {
		addEventListener(Event.ADDED_TO_STAGE, init);
	}

	private function init(e : Event) : void {
		buildUI();
		connectTodataFile();
	}

	private function buildUI() : void {
		stage.align = StageAlign.TOP_LEFT;
		stage.scaleMode = StageScaleMode.NO_SCALE;

		// 建立静态文本
		var inputHere : TextField = new TextField;
		inputHere.width = 120;
		inputHere.height = 20;
		inputHere.text = "在此输入SQL语句：";
		inputHere.x = 5;
		inputHere.y = 5;
		inputHere.selectable = false;
		inputHere.mouseEnabled = false;
		addChild(inputHere);

		// 为输入文本布局
		inTxt.width = 400;
		inTxt.height = 100;
		inTxt.border = true;
		inTxt.x = 130;
		inTxt.y = 5;
		inTxt.type = TextFieldType.INPUT;
		inTxt.wordWrap = true;
		addChild(inTxt);

		// 为按钮布局
		var executeBtn : TextField = new TextField();
		executeBtn.text = "开始执行SQL语句";
		executeBtn.x = 5;
		executeBtn.y = 115;
		executeBtn.addEventListener(MouseEvent.CLICK, runSQL);
		addChild(executeBtn);

		// 为输出文本布局
		outTxt.width = stage.stageWidth;
		outTxt.height = stage.stageHeight - 140;
		outTxt.y = 140;
		outTxt.selectable = false;
		addChild(outTxt);
	}

	private function runSQL(event : MouseEvent) : void {
		if (inTxt.text == "") {
			outTxt.htmlText += "<font size='12' color='#FF0000'>你还没有输入任何SQL语句...<font>";
			return;
		}

		var sql : String = inTxt.text;
		mysqlOperater.text = sql;
		// 开始执行SQL语句
		mysqlOperater.execute();
	}

	private function connectTodataFile() : void {
		mysql = new SQLConnection;
		mysql.addEventListener(SQLEvent.OPEN, openDataFile);
		mysql.addEventListener(SQLErrorEvent.ERROR, error);
		outTxt.htmlText += ("<font size='12' color='#000000'>正在尝试创建数据库文件...<font>");
		mysql.openAsync(dataFile);
	}

	private function openDataFile(event : SQLEvent) : void {
		outTxt.htmlText += "<font size='12' color='#000000'>已经成功连接数据库，数据库文件的路径为：\n<FONT color='#0000FF'><U><a href='http://xiazhihui321.blog.163.com/blog/" + dataFile.parent.nativePath + "'>" + dataFile.nativePath + "</a></U></FONT></font>";

		mysqlOperater = new SQLStatement;
		// 将操作绑定到指定的已经打开的数据库
		mysqlOperater.sqlConnection = mysql;
		mysqlOperater.addEventListener(SQLEvent.RESULT, sqlResult);
		mysqlOperater.addEventListener(SQLErrorEvent.ERROR, sqlError);
	}

	private function sqlResult(event : SQLEvent) : void {
		outTxt.htmlText += "<font size='12' color='#000000'>执行SQL语句成功!!!</font>";

		var result : SQLResult = event.target.getResult();
		if (result.data != null) {
			var numRows : int = result.data.length;

			outTxt.htmlText += "<font size='12' color='#000000'>查询结果为：</font>";
			for (var i : int = 0; i < numRows; i++) {
				var output : String = "";
				for (var columnName : String in result.data[i]) {
					output += columnName + ": " + result.data[i][columnName] + "; ";
				}
				outTxt.htmlText += "<font size='12' color='#000000'>row[" + i.toString() + "]\t" + output + "</font>";
			}
		}
	}

	private function sqlError(event : SQLErrorEvent) : void {
		outTxt.htmlText += "<font size='12'>执行SQL语句失败!!!</font>";
	}

	private function error(event : SQLErrorEvent) : void {
		outTxt.htmlText += "<font size='12'>连接数据库失败!!!</font>";
	}
}
      
   
